package fileanalyser

import (
	"io/fs"
	"os"
	"path/filepath"
	"strings"
)

func GetPicturePaths(rootPath string) ([]string, error) {

	var picture_paths []string

	err := filepath.WalkDir(rootPath, func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}
		if !d.IsDir() {
			ext := filepath.Ext(d.Name())
			ext_lower := strings.ToLower(ext)
			name := path[:len(path)-len(ext)]
			var resampled bool
			if strings.HasSuffix(name, "_resampled") {
				resampled = true
			}
			if (ext_lower == ".png" || ext_lower == ".jpg" || ext_lower == ".jpeg") && !resampled {
				wd, _ := os.Getwd()
				if filepath.IsAbs(path) {
					relpath, err := filepath.Rel(wd, path)
					if err != nil {
						return err
					}
					picture_paths = append(picture_paths, relpath)
				} else {
					picture_paths = append(picture_paths, path)
				}
			}
		}
		return nil
	})
	if err != nil {
		return nil, err
	} else {
		return picture_paths, nil
	}
}
